package com.sander.guardmanagement.ui.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.HomeAddRuzhuren;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.response.ResponseHomeGetUserHomeListData;
import com.sander.guardmanagement.response.ResponseModel;
import com.sander.guardmanagement.until.AppUnit;
import com.sander.guardmanagement.until.UserInfoManager;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

public class AddNewPeopleActivity extends AppCompatActivity {
    ResponseHomeGetUserHomeListData homedata;

    public EditText nameinputView;
    public EditText ageinputView;
    public EditText numpasswordinputview;
    public EditText phoneinputview;
    public Button sex_man_button ;
    public Button sex_woman_button;
    String sex_string = "1";

    public LinearLayout main;
    private int usedsafeheight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_people);
        Intent intent = getIntent();
        homedata = (ResponseHomeGetUserHomeListData) intent.getSerializableExtra("ruzhuren");

        initCallBack();
        initData();
        initView();
        setsexmode(true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

    }

    private void initData(){

    }

    private void initView(){

        nameinputView = (EditText) findViewById(R.id.nameinputview);
        ageinputView = (EditText) findViewById(R.id.ageinputview);
        numpasswordinputview = (EditText) findViewById(R.id.numberinputview);
        phoneinputview = (EditText) findViewById(R.id.phoneinputview);

        final Button bt = (Button)findViewById(R.id.addnewpeoplebutton);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("sander","sure add new people");
                surebutton();
            }
        });
        sex_man_button = (Button) findViewById(R.id.sexmanbutton);
        sex_woman_button = (Button) findViewById(R.id.sexwomanbutton);
        sex_man_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               setsexmode(true);
            }
        });
        sex_woman_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               setsexmode(false);
            }
        });

        main = (LinearLayout)findViewById(R.id.rootoffsetview);
        main.getViewTreeObserver().addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
            @Override
            public void onGlobalFocusChanged(View oldFocus, View newFocus) {
                Rect rect = new Rect();
                main.getWindowVisibleDisplayFrame(rect);
                int mainInvisibleHeight = main.getRootView().getHeight() - (rect.bottom - rect.top);
                Log.d("sander",mainInvisibleHeight + " ----> ");
                if (mainInvisibleHeight > 100) {
                    int[] location = new int[2];
                    bt.getLocationInWindow(location);
                    int srollHeight = (location[1] + bt.getHeight()) - rect.bottom;
                    main.scrollTo(0, srollHeight);
                    usedsafeheight = mainInvisibleHeight;
                } else {
                    main.scrollTo(0, 0);
                    usedsafeheight = 0;
                }

            }
        });

    }
    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    public void setsexmode(boolean model){
        if (model){
            sex_string = "1";
        }else{
            sex_string = "0";
        }
        sex_man_button.setSelected(model);
        sex_woman_button.setSelected(!model);
    }

    public void surebutton(){
        String name_string = nameinputView.getText().toString();
        String age_string = ageinputView.getText().toString();
        String numPassworld_string = numpasswordinputview.getText().toString();
        String phone_string = phoneinputview.getText().toString();

        if (TextUtils.isEmpty(name_string)) showToastMessage("姓名不能为空");
        if (TextUtils.isEmpty(age_string)) showToastMessage("年龄不能为空");
        if (TextUtils.isEmpty(numPassworld_string)) showToastMessage("密码不能为空");
        if (TextUtils.isEmpty(phone_string)) showToastMessage("手机不能为空");
        String sex = sex_string;

        HomeAddRuzhuren homeAddRuzhuren = new HomeAddRuzhuren();
        homeAddRuzhuren.uid = UserInfoManager.sharedManager().getUserInfo().getId() + "";
        homeAddRuzhuren.home_id = homedata.id + "";
        homeAddRuzhuren.user_name = name_string;
        homeAddRuzhuren.age = age_string;
        homeAddRuzhuren.gender = sex + "";
        homeAddRuzhuren.num_password = AppUnit.getSha1(numPassworld_string);
        homeAddRuzhuren.phone = phone_string;

        OkHttpHelper.getinstance().post(homeAddRuzhuren.requestUrl(), homeAddRuzhuren.getValueOfClas(), new BaseCallback<ResponseModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseModel o) {
                Log.d("sander","=>" + o.getMsg());
                if (o.getCode() == 0){
                    finish();
                }else{
                    showToastMessage(o.getMsg());
                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });



    }
    public void showToastMessage(String message){
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

    }
}
